package CGW.controller;

import CGW.bean.Dinner_table;
import CGW.bean.Food;
import CGW.bean.Page;
import CGW.dao.BaseDao;
import CGW.dao.FoodDao;
import CGW.service.FoodService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/userlook")
public class UserLookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FoodService service = new FoodService(new FoodDao());
        String action = req.getParameter("action");

        if (action == null){
            String table_status = req.getParameter("table_status");
            String table_id = req.getParameter("table_id");
            if (table_status.equals("0")){
                String page = req.getParameter("page");
                int pageInt = 1;
                String pageSize = req.getParameter("pageSize");
                int pageSizeInt = 20;
                if(page != null){
                    pageInt = Integer.parseInt(page);
                }
                if (pageSize != null){
                    pageSizeInt = Integer.parseInt(pageSize);
                }
                Page<Food> pageBean2= service.queryAll(pageInt, pageSizeInt);
                System.out.println(pageBean2.getDatas());
                req.setAttribute("pageBean2",pageBean2);
                req.setAttribute("table_id", table_id); // 桌号
                BaseDao<Dinner_table> tableBaseDao = new BaseDao<>();
                Dinner_table table = new Dinner_table(ZHI.zh(table_id),2);
                int i = tableBaseDao.update_one(table);
                if (i>0){
                    tableBaseDao.commit();
                }
                req.getRequestDispatcher("userlook.jsp").forward(req,resp);
            }else {
                req.getRequestDispatcher("/userlook1").forward(req,resp);
            }
        }
    }
}
